package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.ITopicPlanService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.TopicSubjectDetail;
import com.cls.business.service.ITopicSubjectDetailService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 *  Controller
 *
 * @author slx
 * @date 2020-11-20 16:58:28
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class TopicSubjectDetailController extends BaseController {

    private final ITopicSubjectDetailService topicSubjectDetailService;

    private final ITopicPlanService topicPlanService;

    /**
    * 列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSubjectDetail")
    public String topicSubjectDetailIndex(){
        return MyUtil.view("business/topicSubjectDetail/topicSubjectDetail");
    }

    /**
    * 新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSubjectDetail/topicSubjectDetailAdd")
    public String topicSubjectDetailAdd(Model model){
        return MyUtil.view("business/topicSubjectDetail/topicSubjectDetailAdd");
    }

    /**
    * 修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSubjectDetail/topicSubjectDetailUpdate/{topicSubjectDetailId}")
    public String topicSubjectDetailUpdate(@PathVariable Long topicSubjectDetailId, Model model){
        TopicSubjectDetail topicSubjectDetail = topicSubjectDetailService.getById(topicSubjectDetailId);
        model.addAttribute("topicSubjectDetail",topicSubjectDetail);
        return MyUtil.view("business/topicSubjectDetail/topicSubjectDetailUpdate");
    }



    /**
    * 详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicSubjectDetail/topicSubjectDetailDetail/{topicSubjectDetailId}")
    public String topicSubjectDetailDetail(@PathVariable Long topicSubjectDetailId, Model model){
        TopicSubjectDetail topicSubjectDetail = topicSubjectDetailService.getById(topicSubjectDetailId);
        model.addAttribute("topicSubjectDetail",topicSubjectDetail);
        return MyUtil.view("business/topicSubjectDetail/topicSubjectDetailDetail");
    }


    @GetMapping("{planId}/{topicId}/topicSubjectDetail")
    @ResponseBody
//    @RequiresPermissions("topicSubjectDetail:list")
    public ResponseVO topicSubjectDetailList(QueryRequest request, @PathVariable("planId") Long planId, @PathVariable("topicId") Long topicId) {
        TopicPlan topicPlan = topicPlanService.getById(planId);
        if(topicPlan == null){
            throw new IllegalArgumentException("参数异常！");
        }
        TopicSubjectDetail param = new TopicSubjectDetail();
        param.setTopicId(topicId);
        param.setPlanId(planId);
        PageInfo<TopicSubjectDetail> pageInfo =  this.topicSubjectDetailService.findTopicSubjectDetailsByTopicId(request, param,topicPlan.getTopicClassifyCode());
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增TopicSubjectDetail", exceptionMessage = "新增TopicSubjectDetail失败")
    @PostMapping("topicSubjectDetail")
    @ResponseBody
    @RequiresPermissions("topicSubjectDetail:add")
    public ResponseVO addTopicSubjectDetail(@Valid TopicSubjectDetail topicSubjectDetail) {
        this.topicSubjectDetailService.createTopicSubjectDetail(topicSubjectDetail);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除TopicSubjectDetail", exceptionMessage = "删除TopicSubjectDetail失败")
    @PostMapping("topicSubjectDetail/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("topicSubjectDetail:delete")
    public ResponseVO deleteTopicSubjectDetails(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.topicSubjectDetailService.deleteTopicSubjectDetails(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个TopicSubjectDetail", exceptionMessage = "删除TopicSubjectDetail失败")
    @PostMapping("topicSubjectDetail/delete/{topicSubjectDetailId}")
    @ResponseBody
    @RequiresPermissions("topicSubjectDetail:delete")
    public ResponseVO deleteTopicSubjectDetail(@PathVariable Long topicSubjectDetailId) {
        this.topicSubjectDetailService.removeById(topicSubjectDetailId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改TopicSubjectDetail", exceptionMessage = "修改TopicSubjectDetail失败")
    @PostMapping("topicSubjectDetail/update")
    @ResponseBody
    @RequiresPermissions("topicSubjectDetail:update")
    public ResponseVO updateTopicSubjectDetail(TopicSubjectDetail topicSubjectDetail) {
        this.topicSubjectDetailService.updateTopicSubjectDetail(topicSubjectDetail);
        return ResponseVO.success();
    }
}
